﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace FileDuplicateUtility
{
    /// <summary>
    /// Interaction logic for CheckedFileFilter.xaml
    /// </summary>
    public partial class CheckedFileFilter : Window
    {
        public CheckedFileFilter(MainWindow source)
        {
            InitializeComponent();

            Source = source;
        }

        public MainWindow Source { get; private set; }

        public IEnumerable<FilterAction> AvailableActions { get { return Enum.GetValues(typeof(FilterAction)).Cast<FilterAction>(); } }

        public FilterAction SelectedAction
        {
            get { return (FilterAction)GetValue(SelectedActionProperty); }
            set { SetValue(SelectedActionProperty, value); }
        }

        public static readonly DependencyProperty SelectedActionProperty =
            DependencyProperty.Register("SelectedAction", typeof(FilterAction), typeof(CheckedFileFilter), new UIPropertyMetadata(FilterAction.Check));

        public enum FilterAction
        {
            Check,
            Uncheck,
            Toggle
        }

        private void ApplyCmd_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (IncludeFilters.Filters.Count + ExcludeFilters.Filters.Count > 0)
            {
                Source.UpdateProgress(isIndeterminate: true);
                Source.UpdateStatus("Preparing Filters...");

                var action = SelectedAction;

                foreach (var filter in IncludeFilters.Filters)
                {
                    filter.Initialize();
                }

                foreach (var filter in ExcludeFilters.Filters)
                {
                    filter.Initialize();
                }

                ThreadPool.QueueUserWorkItem(state => Apply(action));
            }
        }

        private void Apply(FilterAction action)
        {
            int current = 0, changeCount = 0, total = Source.Duplicates.SelectMany(x => x.Files).Count();

            Source.UpdateProgress(current, total);
            Source.UpdateStatus("Applying Filters...");

            foreach (var file in Source.Duplicates.SelectMany(x => x.Files))
            {
                if (IncludeFilters.Filters.Any(x => x.Predicate(file.File)) &&
                    ExcludeFilters.Filters.All(x => x.Predicate(file.File) == false))
                {
                    MainWindow.UIDispatcher.Invoke((Action)(() =>
                    {
                        switch (action)
                        {
                            case FilterAction.Check:
                                file.IsChecked = true;
                                break;
                            case FilterAction.Uncheck:
                                file.IsChecked = false;
                                break;
                            case FilterAction.Toggle:
                                file.IsChecked = !file.IsChecked;
                                break;
                        }

                        ++changeCount;
                    }));
                }

                Source.UpdateProgress(++current);
            }

            Source.UpdateStatus("Filters Modified {0} Checkboxes", changeCount);
        }
    }
}
